//
//  Log.swift
//  suda
//
//  Created by youai on 2019/7/15.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import CocoaLumberjack

fileprivate func _logInfo(level:DDLogLevel,objList:[Any?]){
    
    if dynamicLogLevel == .off || level.rawValue > dynamicLogLevel.rawValue {
        return
    }
    
    let message = objList.reduce("") { (last, current) -> String in
        let desc : String = ( current == nil ) ? "<nil>" : String(describing: current!)
        return last.count > 0 ? last + " " + desc : desc
    }
//    if Constant.LoggingInConsoleMode {
//        print(message)
//        return
//    }
    DDLogInfo(message,level: level)
}

//参数如果不能懒加载，可能性能不太行
func logDebug(_ obj:Any?...){
    _logInfo(level: .debug, objList: obj)
}

func logInfo(_ obj:Any?...){
    _logInfo(level: .info, objList: obj)
}

func logWarning(_ obj:Any?...){
    _logInfo(level: .warning, objList: obj)
}

func logError(_ obj:Any?...){
    _logInfo(level: .error, objList: obj)
}

func _log1(level:DDLogLevel,_ obj1:@autoclosure ()->Any?){
    _log(level:level) {return [obj1()]}
}
func _log2(level:DDLogLevel,_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?){
    _log(level:level) {return [obj1(),obj2()]}
}
func _log3(level:DDLogLevel,_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?){
    _log(level:level) {return [obj1(),obj2(),obj3()]}
}
func _log4(level:DDLogLevel,_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?,_ obj4:@autoclosure ()->Any?){
    _log(level:level) {return [obj1(),obj2(),obj3(),obj4()]}
}

func logError1(_ obj1:@autoclosure ()->Any?){
    _log(level:.error) {return [obj1()]}
}
func logError2(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?){
    _log(level:.error) {return [obj1(),obj2()]}
}
func logError3(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?){
    _log(level:.error) {return [obj1(),obj2(),obj3()]}
}
func logError4(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?,_ obj4:@autoclosure ()->Any?){
    _log(level:.error) {return [obj1(),obj2(),obj3(),obj4()]}
}

func logInfo1(_ obj1:@autoclosure ()->Any?){
    _log(level:.info) {return [obj1()]}
}
func logInfo2(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?){
    _log(level:.info) {return [obj1(),obj2()]}
}
func logInfo3(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?){
    _log(level:.info) {return [obj1(),obj2(),obj3()]}
}
func logInfo4(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?,_ obj4:@autoclosure ()->Any?){
    _log(level:.info) {return [obj1(),obj2(),obj3(),obj4()]}
}

func logDebug1(_ obj1:@autoclosure ()->Any?){
    _log(level:.debug) {return [obj1()]}
}
func logDebug2(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?){
    _log(level:.debug) {return [obj1(),obj2()]}
}
func logDebug3(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?){
    _log(level:.debug) {return [obj1(),obj2(),obj3()]}
}
func logDebug4(_ obj1:@autoclosure ()->Any?,_ obj2:@autoclosure ()->Any?,_ obj3:@autoclosure ()->Any?,_ obj4:@autoclosure ()->Any?){
    _log(level:.debug) {return [obj1(),obj2(),obj3(),obj4()]}
}

private func _log(level:DDLogLevel,_ listGenerater : ()->[Any?] ){
    if dynamicLogLevel == .off || level.rawValue > dynamicLogLevel.rawValue {
        return
    }
    let message = listGenerater().reduce("") { (last, current) -> String in
        let desc : String = ( current == nil ) ? "<nil>" : String(describing: current!)
        return last.count > 0 ? last + " " + desc : desc
    }
//    if Constant.LoggingInConsoleMode {
//        print(message)
//        return
//    }
    DDLogInfo(message,level: level)
}
